<?php
class UnitTestCase_Unfuddle_Response extends UnitTestCase_Unfuddle {

    public function setUp() {
    
        $this->_responseString = file_get_contents(TESTCASE_PATH . '/files/responses/example.com.txt');
    
    }
    
    public function testConstruct() {
    
        $headers = "Content-Type: application/xml";
        $response = new Unfuddle_Response($headers);
        $this->assertEqual($response->getHeader('content-type'), 'application/xml');
    
    }
    
    public function testSetBody() {
    
        $response = new Unfuddle_Response(null, 'body');
        $this->assertEqual($response->getBody(), 'body');
    
    }
    
    public function testSetStatus() {
    
        $response = new Unfuddle_Response(array('200 OK'));
        $this->assertEqual($response->getStatus(), '200 OK');
    
    }
    
    public function testSetStatusIncludingHttpVersion() {
    
        $response = new Unfuddle_Response();
        $response->setStatus(Unfuddle_Response::HTTP_VERSION . ' 200 OK');
        $this->assertEqual($response->getStatus(), '200 OK');
    
    }
    
    public function testGenerateFromString() {
    
        $response = Unfuddle_Response::generate($this->_responseString);
        $this->assertEqual($response->getStatus(), '200 OK');
        $this->assertEqual($response->getHeader('content-type'), 'text/html; charset=UTF-8');
        // This does not work, but I don't know if it's because of an error or if it's something to do with multibyte chars or something...
        // $this->assertEqual(strlen($response->getBody()), (integer) $response->getHeader('content-length'));
        // This doesn't work because ETag is converted to Etag and because of some other whitespace issues...
        // $this->assertEqual($responseString, (string) $response);
    
    }
    
    public function testGetHeadersAsString() {
    
        $headers = "Server: Apache
Last-Modified: Fri, 30 Jul 2010 15:30:18 GMT
ETag: \"573c1-254-48c9c87349680\"
Accept-Ranges: bytes
Content-Type: text/html; charset=UTF-8
Connection: Keep-Alive
Date: Thu, 13 Jan 2011 00:40:08 GMT
Age: 310
Content-Length: 596
";
        $response = Unfuddle_Response::generate($this->_responseString);
        $this->assertEqual($response->getHeadersAsString(), $headers);
    
    }
    
    public function testHeaderValuesGetTrimmed() {
    
        $response = new Unfuddle_Response(array('Location: http://www.example.com/    '), ' ');
        $response->setHeader('Content-type', '   text/html ');
        $this->assertEqual($response->getHeader('location'), 'http://www.example.com/');
        $this->assertEqual($response->getHeader('content-type'), 'text/html');
    
    }

}